﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Threading.Tasks;
using Centny.Test;
using Windows.UI.Xaml.Media.Imaging;
// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace Centny
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class ImageTest : Centny.Common.LayoutAwarePage
    {
        public ImageTest()
        {
            this.InitializeComponent();
            this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            this._GroupGView.GroupItems = new TestDataSource().RandomTestGrp(5, 20);
            this._GroupGView.ItemsPath = "Objs";
            this._GroupGView.OnPrepareItemContainer = OnPrepareItemContainer;
            await Task.Delay(10);
            this._GroupGView.Width = this.Width;
        }
        public async void OnPrepareItemContainer(GridViewExt gview, DependencyObject element, object item)
        {
            await Task.Delay(10);
            //ScrollViewer sv = new ScrollViewer();
            //Canvas cvs = new Canvas();
            GACItem gi = item as GACItem;
            GridViewItem gvi = element as GridViewItem;
            AutoCanvas.DataTemplateBuilder builder = new AutoCanvas.DataTemplateBuilder();
            builder.Source = new Uri("ms-appx:///Test/AutoHorTemplate.xaml");
            gvi.Width = 800;
            gvi.Height = 700;
            FrameworkElement ui = builder["T3"] as FrameworkElement;
            ui.Height = 800;
            gvi.Content = builder["T3"];
            ui.DataContext = gi.RItems[0];
            //cvs.Children.Add(ui);
            //sv.Content = cvs;
            //sv.VerticalScrollMode = ScrollMode.Disabled;
            //sv.HorizontalScrollMode = ScrollMode.Disabled;
            //sv.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
            //sv.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
            //gvi.Content = sv;
            //sv.Width=cvs.Width=gvi.Width = ui.Width;
            //sv.Height=cvs.Height=gvi.Height = ui.Height=800;
            //AutoCanvas ac;
            //GridViewItem gvi = element as GridViewItem;
            ////if (this.Item2Canvas.ContainsKey(item))
            ////{
            ////    ac = this.Item2Canvas[item];
            ////    gvi.Content = ac;
            ////    return;
            ////}
            //GACItem ritem = item as GACItem;
            //ac = new AutoCanvas();
            //ac.TemplateBuilder.Source = new Uri("ms-appx:///Test/AutoHorTemplate.xaml");
            //ac.LType = AutoCanvas.LayoutType.Horizontal;
            //await Task.Delay(20);
            ////ac.Height = 700;
            //System.Diagnostics.Debug.WriteLine("Width:" + ac.Height);
            //if (ac.Height < 10)
            //{
            //    throw new InvalidDataException("ss");
            //}
            //ac.ItemSources = ritem.RItems;
            //ac.Scrollable = false;
            ////ac.InitialElements();
            ////await Task.Delay(20);
            //gvi.Width = ac.Width;
            //gvi.Height = ac.Height;
            ////gd.Width = ac.Width;
            ////gd.Height = ac.Height;
            ////gd.Children.Add(ac);
            //ac.Visibility = Visibility.Visible;
            //gvi.Content = ac;

        }
        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }
    }
}
